/**
 * OSAmI: Open Source Ambient Intelligence Copyright (C) 2010 UJF. Contact:
 * osami-general@osami-commons.org This library is free software; you can
 * redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or any later version. This library is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU Lesser General Public License for more details. You should have
 * received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.osami.gateway.mediator.sensedata.internal;

import java.util.Hashtable;

import org.osami.gateway.identity.Identity;
import org.osami.gateway.identity.IdentityProvider;
import org.osami.gateway.sensor.data.SenseData;
import org.osgi.service.log.LogService;
import org.osgi.util.measurement.Measurement;

import fr.liglab.adele.cilia.CiliaException;
import fr.liglab.adele.cilia.Data;

public class SenseDataDecorator {

    private LogService m_logger;

    private IdentityProvider m_identityProvider;

    public Data process(Data data) throws CiliaException {
        SenseData in = (SenseData) data.getProperty(SenseData.DEFAULT_DATA_KEY);
        SenseData out = in;
        if (in != null) {
            String senseId = in.getSenseId();
	    int index = senseId.indexOf("/");
	    if (index != -1) {
		senseId = senseId.substring(0, index);
	    }
            if (senseId != null && !senseId.isEmpty()) {
                Identity identity = m_identityProvider.getBoundIdentity(senseId);
                if (identity != null) {
                    String location = (String) identity.getMergedProperties()
                            .get("room.name");
                    if (location != null) {
                        out = new SenseDataImpl(in, location);
                    } else {
                        m_logger.log(LogService.LOG_WARNING,
                                "No location found in identity service found for senseId "
                                        + senseId);
                    }
                } else {
                    m_logger.log(LogService.LOG_WARNING,
                            "No identity found for senseId " + senseId);
                }
            } else {
                m_logger.log(LogService.LOG_ERROR,
                        "No senseId in received SenseData");
            }
        } else {
            m_logger
                    .log(LogService.LOG_ERROR, "No SenseData in received event");
        }
        Hashtable<String, Object> result = new Hashtable<String, Object>();
        result.put(SenseData.DEFAULT_DATA_KEY, out);
        return new Data(null, null, result);
    }

    /**
     * SenseData implementation that is location-aware.
     * @author bourretp
     */
    private class SenseDataImpl implements SenseData {

        private final String m_location;

        private final SenseData m_delegate;

        public SenseDataImpl(SenseData senseData, String location) {
            m_delegate = senseData;
            m_location = location;
        }

        public String getLocation() {
            return m_location;
        }

        public String getSenseId() {
            return m_delegate.getSenseId();
        }

        public Long getTimeStamp() {
            return m_delegate.getTimeStamp();
        }

        public String getType() {
            return m_delegate.getType();
        }

        public String getUnit() {
            return m_delegate.getUnit();
        }

        public Double getValue() {
            return m_delegate.getValue();
        }

        public Measurement toMeasurement() {
            return m_delegate.toMeasurement();
        }

    }

}
